//
//  JJHomeViewController.swift
//  JJSina
//
//  Created by mac on 2017/11/30.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit
private let homeCellID = "homeCellID"
class JJHomeViewController: JJBaseViewController {

    lazy var homeViewModel: JJHomeViewModel = JJHomeViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        // 设置访客视图
        if !isLogin {
            baseView?.setupBaseViewInfo(imageName: nil, title: nil)
            return
        }
        setupUI()
        loadData()
    }
    // MARK: - 添加视图
    private func setupUI() {
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: #selector(leftBtnClick))
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBtnClick))
        setupTableViewInfo()
    }
    // MARK: - 设置tableView的相关信息
    private func setupTableViewInfo() {
        tableView.dataSource = self
        tableView.register(JJHomeTableViewCell.self, forCellReuseIdentifier: homeCellID)
        tableView.rowHeight = UITableViewAutomaticDimension
        // 设置预估行高
        tableView.estimatedRowHeight = 200
        //        tableView.delegate = self
    }

}
// MARK: - tableView的数据源方法
extension JJHomeViewController:  UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.homeViewModel.dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: homeCellID, for: indexPath) as! JJHomeTableViewCell
        cell.statusModel = homeViewModel.dataArray[indexPath.row]
        return cell
    }
}

// MARK: - 请求数据
extension JJHomeViewController {
    func loadData() {
        homeViewModel.getUserHomeData { (isFinish) in
            if !isFinish {
                return
            } else {
                self.tableView.reloadData()
            }
        }
    }
}

// MARK: - 监听方法
extension JJHomeViewController {
    @objc fileprivate func leftBtnClick() {
       
    }
    @objc fileprivate func rightBtnClick() {
        
        let tempVc = JJTempViewController()
        
    navigationController?.pushViewController(tempVc, animated: true)
        
    }
}
